﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Ue.CQRS.Aop;

namespace Ue.CQRS
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
    public class EventHandlerAttribute : Attribute,IAttribute
    {
        public EventHandlerAttribute()
            : this(string.Empty) { }

        public EventHandlerAttribute(string eventName)
        {
            this.EventName = eventName;
        }

        public string EventName { get; private set; }

        public string GetEventName(MethodBase method)
        {
            if (!string.IsNullOrWhiteSpace(this.EventName))
            {
                return this.EventName;
            }

            var eventLeftName = method.Name.EndsWith("Handle")
                ? method.Name.Substring(0, method.Name.Length - 6)
                : method.Name;

            return string.Format("{0}{1}", eventLeftName, "Event");
        }

    }
}
